﻿/**
 * ...
 * @author alijaya
 */
#if flash9
package homponex.core;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;

import heox.Point;
import heox.Rectangle;

class BitmapS9G extends S9G
{

	private var TL:Bitmap;
	private var TC:Bitmap;
	private var TR:Bitmap;
	
	private var ML:Bitmap;
	private var MC:Bitmap;
	private var MR:Bitmap;
	
	private var BL:Bitmap;
	private var BC:Bitmap;
	private var BR:Bitmap;
	
	public var dc(default, null):Class<DisplayObject>;
	
	public function new(dc:Class<DisplayObject>, initRect:Rectangle) 
	{
		this.dc = dc;
		var d:DisplayObject = Type.createInstance(this.dc, []);
		var bd:BitmapData = new BitmapData(Std.int(d.width), Std.int(d.height), true, 0x00000000);
		bd.draw(d);
		var r:Rectangle = Rectangle.haxeRectangle(bd.rect);
		if (!Rectangle.intersects(r, initRect)) throw "Error : bd.rect and r should be intersect";
		
		super(initRect);
		
		TL = new Bitmap();
		TC = new Bitmap();
		TR = new Bitmap();
		
		ML = new Bitmap();
		MC = new Bitmap();
		MR = new Bitmap();
		
		BL = new Bitmap();
		BC = new Bitmap();
		BR = new Bitmap();
		
		var a:Array<Array<Bitmap>> = [[TL, TC, TR], [ML, MC, MR], [BL, BC, BR]];
		var ax:Array<Float> = [0, initRect.left, initRect.right, bd.width];
		var ay:Array<Float> = [0, initRect.top, initRect.bottom, bd.height];
		
		for (j in 0...3)
		{
			for (i in 0...3)
			{
				var cr:Rectangle = new Rectangle(ax[i], ay[j], ax[i + 1] - ax[i], ay[j + 1] - ay[j]);
				var b:Bitmap = a[j][i];
				if (!cr.isEmpty())
				{
					var fr:flash.geom.Rectangle = Rectangle.flashRectangle(cr);
					b.bitmapData = new BitmapData(Std.int(cr.width), Std.int(cr.height), true, 0x00000000);
					b.bitmapData.copyPixels(bd, fr, new flash.geom.Point(0, 0));
				}
				view.addChild(b);
			}
		}
		
		initWidth = bd.width;
		initHeight = bd.height;
		
		dTop = initRect.top;
		dBottom = initHeight - initRect.bottom;
		dLeft = initRect.left;
		dRight = initWidth - initRect.right;
		
		setDim(bd.width, bd.height);
	}
	
	public override function clone() : Point
	{
		var c:BitmapS9G = new BitmapS9G(this.dc, this.initRect);
		return c;
	}
	
	private override function update_width()
	{
		super.update_width();
		
		var a:Array<Bitmap> = [TL, ML, BL];
		var b:Array<Bitmap> = [TC, MC, BC];
		var c:Array<Bitmap> = [TR, MR, BR];
		
		var wl:Float;
		var wc:Float;
		var wr:Float;
		
		if (rect.width <= 0)
		{
			var w:Float = dLeft + dRight;
			wl = dLeft * width / w;
			wc = 0;
			wr = dRight * width / w;
		} else
		{
			wl = dLeft;
			wc = rect.width;
			wr = dRight;
		}
		
		for (n in 0...3)
		{
			var an:Bitmap = a[n];
			var bn:Bitmap = b[n];
			var cn:Bitmap = c[n];
			an.x = 0;
			an.width = wl;
			bn.x = an.x + an.width;
			bn.width = wc;
			cn.x = bn.x + bn.width;
			cn.width = wr;
		}
	}
	
	private override function update_height()
	{
		super.update_height();
		
		var a:Array<Bitmap> = [TL, TC, TR];
		var b:Array<Bitmap> = [ML, MC, MR];
		var c:Array<Bitmap> = [BL, BC, BR];
		
		var ht:Float;
		var hm:Float;
		var hb:Float;
		
		if (rect.height <= 0)
		{
			var h:Float = dTop + dBottom;
			ht = dTop * height / h;
			hm = 0;
			hb = dBottom * height / h;
		} else
		{
			ht = dTop;
			hm = rect.height;
			hb = dBottom;
		}
		
		for (n in 0...3)
		{
			var an:Bitmap = a[n];
			var bn:Bitmap = b[n];
			var cn:Bitmap = c[n];
			an.y = 0;
			an.height = ht;
			bn.y = an.y + an.height;
			bn.height = hm;
			cn.y = bn.y + bn.height;
			cn.height = hb;
		}
	}
	
}
#end